//
//  EMGeeVodViewController.m
//  PictureSelector
//
//  Created by Lyy on 15/8/21.
//  Copyright (c) 2015年 Lyy. All rights reserved.
//

#import "EMGeeVodViewController.h"

#if !TARGET_IPHONE_SIMULATOR
#import <genseeFramework/VodSdk.h>
#import <genseeFramework/downItem.h>
#endif

@interface EMGeeVodViewController ()

#if !TARGET_IPHONE_SIMULATOR
<VodDownLoadDelegate,
VodPlayDelegate
>
{
    downItem *_currentDownItem;
}
@property(nonatomic,strong)VodDownLoader *voddownloader;
@property(nonatomic,strong)VodPlayer *vodplayer;
#endif

@end

@implementation EMGeeVodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
#if !TARGET_IPHONE_SIMULATOR
    [self createVodDownloader];
#endif

}

#if !TARGET_IPHONE_SIMULATOR
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.vodplayer stop];
    self.vodplayer = nil;
    [[ UIApplication sharedApplication] setIdleTimerDisabled:NO] ;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.vodplayer.mVideoView.frame = self.videoView.frame;
    if (self.vodplayer.mVideoView.movieASImageView) {
        self.vodplayer.mVideoView.movieASImageView.frame = self.vodplayer.mVideoView.frame;
    }
}
#endif

#if !TARGET_IPHONE_SIMULATOR
- (void)createVodDownloader {
    self.voddownloader = [[VodDownLoader alloc]init];
    self.voddownloader.delegate = self;
    [self setVodProgram];
}

- (void)setVodProgram
{
    [self.voddownloader addItem:@"emoney.gensee.com"
                         number:@"64657833"
                      loginName:@"888888"
                    vodPassword:@"888888"
                  loginPassword:@"888888"
                       downFlag:0
                        serType:@"webcast"];
    
}

#pragma mark -
#pragma mark VodDownLoadDelegate
//添加item的回调方法
-(void)onAddItemResoult:(RESULT_TYPE)resoultType voditem:(downItem *)item
{
    if (resoultType == RESULT_SUCCESS) {                   //如果添加成功，则开始下载
        //[self.voddownloader Downlod:item];
        _currentDownItem = item;
        [self startPlay:item.strDownloadID];
    }
}

- (void)startPlay:(NSString *)vodId {
    
    downItem *downitem = [[VodManage shareManage] findDownItem:vodId];
    if (downitem) {
        if (self.vodplayer) {
            [self.vodplayer stop];
            self.vodplayer = nil;
        }
        self.vodplayer = [[VodPlayer alloc] initPlay:self videoViewFrame:self.view.bounds docViewFrame:CGRectZero downitem:downitem playDelegate:self];
        [self.vodplayer setVideoFrame:self.view.bounds];
        self.vodplayer.mVideoView.contentMode = UIViewContentModeScaleAspectFit;
        [self.vodplayer OnlinePlay];
        [self.view insertSubview:self.vodplayer.mVideoView belowSubview:self.videoView];
    }
}

#pragma mark -

//播放按钮的方法
- (void) onInit:(int) result haveVideo:(BOOL)haveVideo duration:(int) duration docInfos:(NSDictionary*)docInfos
//播放文件的信息.result表示播放的结果,havevideo表示是否包含视频.duration表示文件时长。docinfos表示文档信息
{
    NSLog(@"havevideo:%d,duration:%d,docinfos:%@",haveVideo,duration,docInfos);
    
    if (haveVideo) {
        self.bottomBarView.totalTime = duration;
    }
    self.vodplayer.mVideoView.contentMode = UIViewContentModeScaleAspectFit;
    [self.bottomBarView setTotalTime:duration];
}

- (void)onPosition:(int)position {
    [self.bottomBarView bringSubviewToFront:self.vodplayer.mVideoView];
    self.bottomBarView.hadPlayTime = position;
}

- (void)bottomBarView:(BottomBarView *)view isPlaying:(BOOL)isPlaying {
    [super bottomBarView:view isPlaying:isPlaying];
    if (isPlaying) {
        [self.vodplayer resume];
    } else {
        [self.vodplayer pause];
    }
}

- (void)bottomBarView:(BottomBarView *)view slidePosition:(CGFloat)currentPosition {
    [super bottomBarView:view slidePosition:currentPosition];
    [self.vodplayer seekTo:currentPosition];
}

-(void)deleteItem
{
    [self.voddownloader delete:_currentDownItem.strDownloadID];               //根据ID删除downitem
    downItem *item = [[VodManage shareManage] findDownItem:_currentDownItem.strDownloadID];    //根据ID找到downitem
    if (!item) {
        NSLog(@"delete successs");
    }
}

- (void)dealloc {
    [self deleteItem];
}

#endif

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


